/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.fields;

import com.google.gwt.dom.client.FormElement;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.dom.XElement;
import com.sencha.gxt.widget.core.client.Component;
import com.sencha.gxt.widget.core.client.container.FlowLayoutContainer;
import com.sencha.gxt.widget.core.client.container.MarginData;
import com.sencha.gxt.widget.core.client.form.FormPanel;



/**
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkSingleFieldFormPanel extends FormPanel
{
	private final FlowLayoutContainer formPanelFlowLayoutContainer;

	/**
	 * Constructor
	 */
	public DkSingleFieldFormPanel()
	{
		formPanelFlowLayoutContainer = new FlowLayoutContainer();
		super.add(formPanelFlowLayoutContainer);
		addResizeHandler(new ResizeHandler()
		{
			@Override
			public void onResize(final ResizeEvent event)
			{
				final int width = event.getWidth();
				if (width > 0)
				{
					for (int i = 0; i < formPanelFlowLayoutContainer.getWidgetCount(); i++)
					{
						final Widget widget = formPanelFlowLayoutContainer.getWidget(i);
						if (widget instanceof Component)
						{
							((Component) widget).setWidth(width);
						}
					}
				}
			}
		});
	}

	/**
	 * @return the underlying {@link FormElement}
	 */
	public FormElement getFormElement()
	{
		final XElement element = getElement();
		return FormElement.as(element);
	}

	/**
	 * Set the accepted responses types
	 * 
	 * @param encodingListCommaSeparated
	 *           a comma separated list of types (ex: application/json,text/html,application/xhtml+xml,application/xml)
	 */
	public void setAccept(final String encodingListCommaSeparated)
	{
		getElement().setAttribute("accept", encodingListCommaSeparated);
	}

	@Override
	public void add(final IsWidget child)
	{
		formPanelFlowLayoutContainer.add(child);
	}

	@Override
	public void add(final Widget child)
	{
		formPanelFlowLayoutContainer.add(child);
	}

	@Override
	public void add(final Widget child, final MarginData layoutData)
	{
		formPanelFlowLayoutContainer.add(child, layoutData);
	}

}
